package com.bistu.notepad_zjr;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bistu.notepad_zjr.bean.Note;
import com.bistu.notepad_zjr.util.SpfUtil;
import com.bistu.notepad_zjr.util.TimeChange;

import java.text.ParseException;
import java.util.Calendar;

import static java.lang.String.format;

public class EditActivity extends AppCompatActivity {

    private Note note;
    private EditText etTitle,etContent,etEndTime,etAuthor;
    private NoteDbOpenHelper mNoteDbOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);
        etEndTime = findViewById(R.id.et_endtime);
        etAuthor = findViewById(R.id.et_author);    //作者
        initData();
    }

    private void initData() {
//        //加载作者
//        etAuthor.setText(SpfUtil.loadSPF(this,"Author"));

        Intent intent = getIntent();
        //接收note
        note = (Note) intent.getSerializableExtra("note");
        if (note != null) {
            etTitle.setText(note.getTitle());
            etContent.setText(note.getContent());
            etAuthor.setText(note.getAuthor());
            if (note.getEndTime() == 0){
                etEndTime.setText("");
            }else {
                etEndTime.setText(TimeChange.timeToStr(note.getEndTime()));
            }

        }
        mNoteDbOpenHelper = new NoteDbOpenHelper(this);
    }

    public void save(View view) throws ParseException {
        String title = etTitle.getText().toString();
        String content = etContent.getText().toString();
        String endTime = etEndTime.getText().toString();
        String author = etAuthor.getText().toString();

//        //保存作者
//        SpfUtil.saveSPF(this,"Author",author);

        if (TextUtils.isEmpty(title)) {
            Toast.makeText(this,"标题不能为空",Toast.LENGTH_SHORT);
            return;
        }
        note.setTitle(title);
        note.setAuthor(author);
        note.setContent(content);
        note.setEndTime(TimeChange.strToTime(endTime));
//        note.setCreatedTime(getCurrentTimeFormat());
        //更新数据
        long rowId = mNoteDbOpenHelper.updateData(note);
        if (rowId != -1) {
            Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT);
            this.finish();
        }else{
            Toast.makeText(this,"修改失败",Toast.LENGTH_SHORT);
        }

    }

    //选择时间
    public void selectTime(View view) {
        //获取时间
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog( this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String timeStr = format("%d-%d-%d",year,month+1,dayOfMonth);
                try {
                    //选择的时间戳
                    long timeVal = TimeChange.strToTime(timeStr);
                    long createTime = TimeChange.strToTime(note.getCreatedTime());
                    if (timeVal < createTime){
                        Toast.makeText(EditActivity.this,"时间选择错！",Toast.LENGTH_SHORT).show();
                    }else {
                        String endTimeStr = TimeChange.timeToStr(timeVal);
                        etEndTime.setText(endTimeStr);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
                ,calendar.get(Calendar.YEAR)
                ,calendar.get(Calendar.MONTH)
                ,calendar.get(Calendar.DAY_OF_MONTH)).show();
    }
}